package cn.patpang.app.timer;

import cn.patpang.app.entity.Demo;
import cn.patpang.app.service.impl.DemoServiceImpl;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class Timer {

    private static boolean priceTimerFlag = false;

    @Autowired
    private DemoServiceImpl demoServiceImpl;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void priceTimer() {
        if (priceTimerFlag) {
            return;
        }

        System.out.println(" run timer ");
        try {
            // select
            JSON.toJSONString(demoServiceImpl.list());

            // add
            demoServiceImpl.save(new Demo("demoString", new Date(), "demoText", 9, 34, "noneString"));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            priceTimerFlag = false;
        }
    }

}